package com.midian.common.utils;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

public class GeoAPI {
	/**百度地图AK*/
	public final static String AK ="8f740012d2133b7922d7cf107c4e4940";

	public static Map getLocationByLatLng(String lat,String lng){
		Map<String,String> locInfo= new HashMap<String,String>();
		String url="http://api.map.baidu.com/geocoder/v2/?ak="+AK+"&callback=renderReverse&location="+lat+","+lng+"&output=json&pois=0";
		JSONObject retmsgJson=HttpClientUtils.getHttpResJson(url, "GET");
		if(!retmsgJson.getString("status").equals("0")){
			return null;
		}
		JSONObject address=JSONObject.fromObject( retmsgJson.get("result"));
		JSONObject addressComponent=JSONObject.fromObject( address.get("addressComponent"));
		locInfo.put("address",address.getString("formatted_address"));	
		locInfo.put("city", addressComponent.getString("city").substring(0, addressComponent.getString("city").length()-1));//市
		locInfo.put("district", addressComponent.getString("district"));//区
		locInfo.put("province", addressComponent.getString("province").substring(0, addressComponent.getString("province").length()-1));//省
		
		return locInfo;
	}
	public static Map getLatLngByLocation(String loc,String cityName){
		Map<String,String> geoInfo= new HashMap<String,String>();
		String url="http://api.map.baidu.com/geocoder/v2/?ak="+AK+"&callback=renderOption&output=json&address="+loc+"&city="+cityName;
		JSONObject retmsgJson=HttpClientUtils.getHttpResJson(url, "GET");
		if(!retmsgJson.getString("status").equals("0")){
			return null;
		}
		JSONObject result=JSONObject.fromObject( retmsgJson.get("result"));
		JSONObject location=JSONObject.fromObject(result.get("location"));
		geoInfo.put("lat", location.getString("lat").substring(0, location.getString("lat").length()-6));
		geoInfo.put("lng", location.getString("lng").substring(0, location.getString("lat").length()-5));
		return geoInfo;
	}
//	public static void main(String[] args) {
//		GeoAPI geoapi= new GeoAPI();
//		Map<String,String> info=geoapi.getLatLngByLocation("广州市天河区天朗明居BRT", "广州");
//		System.out.println(info.get("lng"));
//		
//	}
}
